<?php
namespace App\Models\Scripts;

use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class v25e64aa754c310d45c1e084d574c1bb0 extends Sysupdate
{
    protected $version_id ='0.0.1';

    private $table_name = 'sys_version';
    private $returnData = array('code'=>0,'message'=>'执行成功');

    public function __construct()
    {
        parent::__construct();
    }

    public function upgrade()
    {
        $r_data = $this->_step_1();
        if($r_data['code']>0){
            return $r_data;
        }
        $r_data = $this->_step_2();
        if($r_data['code']>0){
            return $r_data;
        }
        $r_data = $this->_step_3();
        if($r_data['code']>0){
            return $r_data;
        }
        $tmp_data = $r_data['data'];
        $r_data = $this->_step_4();
        if($r_data['code']>0){
            return $r_data;
        }
        $r_data['data'] = $tmp_data;
        return $r_data;
    }

    //添加路由 admin_permissions
    private function _step_1()
    {
        $res = DB::table('admin_permissions')->where('name','LIKE','%admin.achievement.index%')->first();
        $res = json_decode(json_encode($res),true);
//        $data = array(
//            'name' => 'admin.achievement.getdatalist',
//            'label' => '获取订单数据',
//            'cid' => $res['id'],
//            'display' => 0,
//            'sort' => 0,
//            'created_at' => $this->time,
//            'updated_at' => $this->time
//        );
//        $flag = DB::table('admin_permissions')->insert($data);
//        if(!$flag){
//            $this->returnData['code'] = 99;
//            $this->returnData['message'] = '添加admin.achievement.getdatalist失败';
//            return $this->returnData;
//        }
        $data = array(
            'name' => 'admin.achievement.updaterecord',
            'label' => '更新订单信息',
            'cid' => $res['id'],
            'display' => 0,
            'sort' => 0,
            'created_at' => $this->time,
            'updated_at' => $this->time
        );
        $flag = DB::table('admin_permissions')->insert($data);
        if(!$flag){
            $this->returnData['code'] = 99;
            $this->returnData['message'] = '添加admin.achievement.updaterecord失败';
            return $this->returnData;
        }
        $data = array(
            'name' => 'admin.achievement.updatedetail',
            'label' => '更新订单详情',
            'cid' => $res['id'],
            'display' => 0,
            'sort' => 0,
            'created_at' => $this->time,
            'updated_at' => $this->time
        );
        $flag = DB::table('admin_permissions')->insert($data);
        if(!$flag){
            $this->returnData['code'] = 99;
            $this->returnData['message'] = '添加admin.achievement.updatedetail失败';
            return $this->returnData;
        }
//        $res = DB::table('admin_permissions')->where('cid',0)->where('name','LIKE','%admin.aftersale%')->first();
//        $res = json_decode(json_encode($res),true);
//        $data = array(
//            'name' => 'admin.aftersale.getdatalist',
//            'label' => '获取售后数据',
//            'cid' => $res['id'],
//            'display' => 0,
//            'sort' => 0,
//            'created_at' => $this->time,
//            'updated_at' => $this->time
//        );
//        $flag = DB::table('admin_permissions')->insert($data);
//        if(!$flag){
//            $this->returnData['code'] = 99;
//            $this->returnData['message'] = '添加admin.aftersale.getdatalist失败';
//            return $this->returnData;
//        }
        $data = array(
            'name' => 'admin.hr',
            'label' => '人事管理',
            'description' => '人事管理列表',
            'cid' => 0,
            'icon' => 'fa-steam',
            'display' => 0,
            'sort' => 0,
            'created_at' => $this->time,
            'updated_at' => $this->time
        );
        $insert_id = DB::table('admin_permissions')->insertGetId($data);
        if(!$insert_id || $insert_id<1){
            $this->returnData['code'] = 99;
            $this->returnData['message'] = '添加admin.hr失败';
            return $this->returnData;
        }
        $data = array(
            'name' => 'admin.hr.index',
            'label' => '员工列表',
            'cid' => $insert_id,
            'display' => 0,
            'sort' => 0,
            'created_at' => $this->time,
            'updated_at' => $this->time
        );
        $res = DB::table('admin_permissions')->insert($data);
        if(!$res || $res<1){
            $this->returnData['code'] = 99;
            $this->returnData['message'] = '添加admin.hr.index失败';
            return $this->returnData;
        }
        $data = array(
            'name' => 'admin.hr.edit',
            'label' => '员工资料编辑',
            'cid' => $insert_id,
            'display' => 0,
            'sort' => 0,
            'created_at' => $this->time,
            'updated_at' => $this->time
        );
        $res = DB::table('admin_permissions')->insert($data);
        if(!$res || $res<1){
            $this->returnData['code'] = 99;
            $this->returnData['message'] = '添加admin.hr.edit';
            return $this->returnData;
        }
//        $data = array(
//            'name' => 'admin.hr.getdatalist',
//            'label' => '获取员工数据',
//            'cid' => $insert_id,
//            'display' => 0,
//            'sort' => 0,
//            'created_at' => $this->time,
//            'updated_at' => $this->time
//        );
//        $res = DB::table('admin_permissions')->insertGetId($data);
//        if(!$res){
//            $this->returnData['code'] = 99;
//            $this->returnData['message'] = '添加admin.hr.getdatalist失败';
//            return $this->returnData;
//        }
        return $this->returnData;
    }

    private function _step_2()
    {
        $res = DB::table('admin_permissions')->where('name','LIKE','%admin.achievement.index%')->first();
        $res = json_decode(json_encode($res),true);
        $data = array(
            'name' => 'admin.achievement.exchangeorder',
            'label' => '业绩订单转移',
            'cid' => $res['id'],
            'display' => 0,
            'sort' => 0,
            'created_at' => $this->time,
            'updated_at' => $this->time
        );
        $flag = DB::table('admin_permissions')->insert($data);
        if(!$flag){
            $this->returnData['code'] = 99;
            $this->returnData['message'] = '添加admin.achievement.exchangeorder失败';
            return $this->returnData;
        }
        $res = DB::table('admin_permissions')->where('name','LIKE','%admin.aftersale.index%')->first();
        $res = json_decode(json_encode($res),true);
        $data = array(
            'name' => 'admin.aftersale.exchangeorder',
            'label' => '售后订单转移',
            'cid' => $res['id'],
            'display' => 0,
            'sort' => 0,
            'created_at' => $this->time,
            'updated_at' => $this->time
        );
        $flag = DB::table('admin_permissions')->insert($data);
        if(!$flag){
            $this->returnData['code'] = 99;
            $this->returnData['message'] = '添加admin.aftersale.exchangeorder失败';
            return $this->returnData;
        }
//        $res = DB::table('admin_permissions')->where('name','LIKE','%admin.bonuslog.index%')->first();
//        $res = json_decode(json_encode($res),true);
//        $data = array(
//            'name' => 'admin.bonuslog.getdatalist',
//            'label' => '获取提成数据',
//            'cid' => $res['id'],
//            'display' => 0,
//            'sort' => 0,
//            'created_at' => $this->time,
//            'updated_at' => $this->time
//        );
//        $flag = DB::table('admin_permissions')->insert($data);
//        if(!$flag){
//            $this->returnData['code'] = 99;
//            $this->returnData['message'] = '添加admin.bonuslog.getdatalist失败';
//            return $this->returnData;
//        }
        return $this->returnData;
    }

    private function _step_3()
    {
//        $res = DB::table('admin_permissions')->where('cid',0)->where('name','LIKE','%admin.article%')->first();
//        $res = json_decode(json_encode($res),true);
//        $data = array(
//            'name' => 'admin.articles.getdatalist',
//            'label' => '获取文章数据',
//            'cid' => $res['id'],
//            'display' => 0,
//            'sort' => 0,
//            'created_at' => $this->time,
//            'updated_at' => $this->time
//        );
//        $flag = DB::table('admin_permissions')->insert($data);
//        if(!$flag){
//            $this->returnData['code'] = 99;
//            $this->returnData['message'] = 'admin.articles.getdatalist';
//            return $this->returnData;
//        }
//
//        /* 插入admin.branch.getdatalist 路由 */
//        $res = DB::table('admin_permissions')->where('name','LIKE','%admin.branch.index%')->first();
//        $res = json_decode(json_encode($res),true);
//        $data = array(
//            'name' => 'admin.branch.getdatalist',
//            'label' => '获取团队数据',
//            'cid' => $res['id'],
//            'display' => 0,
//            'sort' => 0,
//            'created_at' => $this->time,
//            'updated_at' => $this->time
//        );
//        $flag = DB::table('admin_permissions')->insert($data);
//        if(!$flag){
//            $this->returnData['code'] = 99;
//            $this->returnData['message'] = 'admin.branch.getdatalist';
//            return $this->returnData;
//        }
//
//        /* 插入admin.company.getdatalist 路由 */
//        $res = DB::table('admin_permissions')->where('name','LIKE','%admin.company.index%')->first();
//        $res = json_decode(json_encode($res),true);
//        $data = array(
//            'name' => 'admin.company.getdatalist',
//            'label' => '获取公司数据',
//            'cid' => $res['id'],
//            'display' => 0,
//            'sort' => 0,
//            'created_at' => $this->time,
//            'updated_at' => $this->time
//        );
//        $flag = DB::table('admin_permissions')->insert($data);
//        if(!$flag){
//            $this->returnData['code'] = 99;
//            $this->returnData['message'] = 'admin.company.getdatalist';
//            return $this->returnData;
//        }
//
//        /* 插入admin.customer.getdatalist 路由 */
//        $res = DB::table('admin_permissions')->where('name','LIKE','%admin.customer.index%')->first();
//        $res = json_decode(json_encode($res),true);
//        $data = array(
//            'name' => 'admin.customer.getdatalist',
//            'label' => '获取客户数据',
//            'cid' => $res['id'],
//            'display' => 0,
//            'sort' => 0,
//            'created_at' => $this->time,
//            'updated_at' => $this->time
//        );
//        $flag = DB::table('admin_permissions')->insert($data);
//        if(!$flag){
//            $this->returnData['code'] = 99;
//            $this->returnData['message'] = 'admin.customer.getdatalist';
//            return $this->returnData;
//        }
//
//        /* 插入admin.goods.getdatalist 路由 */
//        $res = DB::table('admin_permissions')->where('name','LIKE','%admin.goods.index%')->first();
//        $res = json_decode(json_encode($res),true);
//        $data = array(
//            'name' => 'admin.goods.getdatalist',
//            'label' => '获取商品数据',
//            'cid' => $res['id'],
//            'display' => 0,
//            'sort' => 0,
//            'created_at' => $this->time,
//            'updated_at' => $this->time
//        );
//        $flag = DB::table('admin_permissions')->insert($data);
//        if(!$flag){
//            $this->returnData['code'] = 99;
//            $this->returnData['message'] = 'admin.goods.getdatalist';
//            return $this->returnData;
//        }
//
//        /* 插入admin.memberlevel.getdatalist 路由 */
//        $res = DB::table('admin_permissions')->where('name','LIKE','%admin.memberlevel.index%')->first();
//        $res = json_decode(json_encode($res),true);
//        $data = array(
//            'name' => 'admin.memberlevel.getdatalist',
//            'label' => '获取等级数据',
//            'cid' => $res['id'],
//            'display' => 0,
//            'sort' => 0,
//            'created_at' => $this->time,
//            'updated_at' => $this->time
//        );
//        $flag = DB::table('admin_permissions')->insert($data);
//        if(!$flag){
//            $this->returnData['code'] = 99;
//            $this->returnData['message'] = 'admin.memberlevel.getdatalist';
//            return $this->returnData;
//        }
//
//        /* 插入admin.membersource.getdatalist 路由 */
//        $res = DB::table('admin_permissions')->where('name','LIKE','%admin.membersource.index%')->first();
//        $res = json_decode(json_encode($res),true);
//        $data = array(
//            'name' => 'admin.membersource.getdatalist',
//            'label' => '获取来源数据',
//            'cid' => $res['id'],
//            'display' => 0,
//            'sort' => 0,
//            'created_at' => $this->time,
//            'updated_at' => $this->time
//        );
//        $flag = DB::table('admin_permissions')->insert($data);
//        if(!$flag){
//            $this->returnData['code'] = 99;
//            $this->returnData['message'] = 'admin.membersource.getdatalist';
//            return $this->returnData;
//        }
//
//        /* 插入admin.member.getdatalist 路由 */
//        $res = DB::table('admin_permissions')->where('name','LIKE','%admin.member.index%')->first();
//        $res = json_decode(json_encode($res),true);
//        $data = array(
//            'name' => 'admin.member.getdatalist',
//            'label' => '获取客户数据',
//            'cid' => $res['id'],
//            'display' => 0,
//            'sort' => 0,
//            'created_at' => $this->time,
//            'updated_at' => $this->time
//        );
//        $flag = DB::table('admin_permissions')->insert($data);
//        if(!$flag){
//            $this->returnData['code'] = 99;
//            $this->returnData['message'] = 'admin.member.getdatalist';
//            return $this->returnData;
//        }
//
//        /* 插入admin.order.getdatalist 路由 */
//        $res = DB::table('admin_permissions')->where('name','LIKE','%admin.order.index%')->first();
//        $res = json_decode(json_encode($res),true);
//        $data = array(
//            'name' => 'admin.order.getdatalist',
//            'label' => '获取订单数据',
//            'cid' => $res['id'],
//            'display' => 0,
//            'sort' => 0,
//            'created_at' => $this->time,
//            'updated_at' => $this->time
//        );
//        $flag = DB::table('admin_permissions')->insert($data);
//        if(!$flag){
//            $this->returnData['code'] = 99;
//            $this->returnData['message'] = 'admin.order.getdatalist';
//            return $this->returnData;
//        }
//
//        /* 插入admin.permission.getdatalist 路由 */
//        $res = DB::table('admin_permissions')->where('name','LIKE','%admin.permission.index%')->first();
//        $res = json_decode(json_encode($res),true);
//        $data = array(
//            'name' => 'admin.permission.getdatalist',
//            'label' => '获取权限数据',
//            'cid' => $res['id'],
//            'display' => 0,
//            'sort' => 0,
//            'created_at' => $this->time,
//            'updated_at' => $this->time
//        );
//        $flag = DB::table('admin_permissions')->insert($data);
//        if(!$flag){
//            $this->returnData['code'] = 99;
//            $this->returnData['message'] = 'admin.permission.getdatalist';
//            return $this->returnData;
//        }
        $res = $this->clearCache();
        $this->returnData['data'] = $res;
        return $this->returnData;
    }

    private function _step_4()
    {
        $data = array(
            'version' => $this->version_id,
            'status' => 2,
            'tips' => $this->_buildTips(),
            'introduce' => $this->_buildIntroduce(),
            'create_time' => $this->timestamp
        );
        $res = DB::table($this->table_name)->insert($data);
        if(!$res){
            $this->returnData['code'] = 99;
            $this->returnData['message'] = $this->version_id.'更新失败';
            return $this->returnData;
        }
        return $this->returnData;
    }

    /* 修复v0.0.1 时间引入错误导致数据库记录时间错误的bug */
    private function _fix_script()
    {
        $res = DB::table('admin_permissions')->where('name','LIKE','%admin.achievement.updaterecord%')->first();
        $res = json_decode(json_encode($res),true);
        $f_id = $res['id'];
        $data = array(
            'created_at' => $this->time,
            'updated_at' => $this->time
        );
        $res = DB::table('admin_permissions')->where('id','>',$f_id)->update($data);
        if(!$res){
            $this->returnData['code'] = 101;
            $this->returnData['message'] = '修复admin_permissions时间失败';
            return $this->returnData;
        }
        $data = array(
            'create_time' => $this->timestamp
        );
        $res = DB::table('sys_version')->update($data);
        if(!$res){
            $this->returnData['code'] = 101;
            $this->returnData['message'] = '修复sys_version时间失败';
            return $this->returnData;
        }
        return $this->returnData;
    }

    private function _buildTips()
    {
        $content = "修改php.ini disable_functions里去掉proc_open<br>version 1.0.0之前的版本为测试版本";
        return $content;
    }

    private function _buildIntroduce()
    {
        $html_str = '<ul>';
        $html_str .= '    <li>';
        $html_str .= '        <h3>1.版本介绍</h3>';
        $html_str .= '        <ul>';
        $html_str .= '            <li>1.1 基类增加运行shell命令以及回显的方法</li>';
        $html_str .= '            <li>1.2 基类增加更新路由的脚本</li>';
        $html_str .= '            <li>1.3 添加此次代码更新需要添加的路由</li>';
        $html_str .= '        </ul>';
        $html_str .= '    </li>';
        $html_str .= '</ul>';
        return $html_str;
    }
}
